package com.ruanjian.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.ruanjian.eduservice.entity.EduSubject;
import com.ruanjian.eduservice.entity.EduTeacher;
import com.ruanjian.eduservice.entity.excel.subjectData;
import com.ruanjian.eduservice.entity.suject.OneSubject;
import com.ruanjian.eduservice.entity.suject.TwoSubject;
import com.ruanjian.eduservice.listener.subjectListen;
import com.ruanjian.eduservice.mapper.EduSubjectMapper;
import com.ruanjian.eduservice.mapper.EduTeacherMapper;
import com.ruanjian.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-07
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, subjectData.class,new subjectListen(eduSubjectService)).sheet().doRead();

        } catch (IOException e) {
            e.printStackTrace();

        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //一级分类
        QueryWrapper<EduSubject> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id","0");
        List<EduSubject> oneEduSubjects = baseMapper.selectList(queryWrapper);

        //二级分类
        QueryWrapper<EduSubject> queryWrapper2=new QueryWrapper<>();
        queryWrapper.ne("parent_id","0");
        List<EduSubject> twoeduSubjects = baseMapper.selectList(queryWrapper2);


        //最终封装数据
        List<OneSubject> finalSubject=new ArrayList<>();

        //封装一级分类
        for(EduSubject  eduSubject:oneEduSubjects)
        {
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);
            List<TwoSubject> children=new ArrayList<>();
            for (EduSubject eduSubject1:twoeduSubjects)
            {
                if (eduSubject1.getParentId().equals(eduSubject.getId()))
                {
                    TwoSubject twoSubject=new TwoSubject();

                    BeanUtils.copyProperties(eduSubject1,twoSubject);

                    children.add(twoSubject);

                }

            }
            oneSubject.setChildren(children);
            finalSubject.add(oneSubject);

        }

        return finalSubject;



    }

}
